﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Org.Hessian.Utils;

namespace SmartMD.DataAccess.Service
{
    /// <summary>
    /// 分页查询类
    /// </summary>
    [DataContract(Name = "PageQuery", Namespace = "")]
    public class PagingQuery
    {
        private Query _query;
        private string _entityName;

        /// <summary>
        /// 创建分页查询对象
        /// </summary>
        public PagingQuery()
        {
            ExcludeFields = new DataContractStringCollection();
            IncludingEntities = new DataContractStringCollection();
            IncludingEntitieTypes = new List<Type>();
        }

        public PagingQuery(Type type)
            : this(ClassType.GetTypeName(type))
        {

        }

        public PagingQuery(string entityName)
            : this()
        {
            _entityName = entityName;
        }

        /// <summary>
        /// 分页大小
        /// </summary>
        [DataMember(Name = "pageSize")]
        public int PageSize { get; set; }

        /// <summary>
        /// 当前页索引
        /// </summary>
        [DataMember(Name = "currentPage")]
        public int CurrentPageIndex { get; set; }

        /// <summary>
        /// 查询条件
        /// </summary>
        [DataMember(Name = "conditionXml")]
        public string ConditionString
        {
            get
            {
                if (Query != null)
                {
                    return Query.ToString();
                }

                return string.Empty;
            }
            set { }
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        [IgnoreDataMember]
        public Query Query
        {
            get
            {
                if (_query == null)
                    _query = new Query(_entityName);
                return _query;
            }
            set { _query = value; }
        }

        /// <summary>
        /// 排除的字段（这些字段将不会出现在结果中）
        /// </summary>
        [DataMember(Name = "filterFields")]
        public DataContractStringCollection ExcludeFields { get; private set; }

        /// <summary>
        /// 包含的实体（这些实体将会作为嵌套元素出现在返回结果中）
        /// </summary>
        [DataMember(Name = "loadEntities")]
        public DataContractStringCollection IncludingEntities { get; private set; }

        [DataMember(Name = "loadEntitieTypes")]
        public List<Type> IncludingEntitieTypes { get; private set; }

        public override string ToString()
        {
            return this.ConditionString;
        }
    }
}
